import params from './params'
export function realPx(px) {
  const maxWidth = window.innerWidth > 500 ? 500 : window.innerWidth
  return px * (maxWidth / 375)
}

export function addStyle(link) {
  let head = document.getElementsByTagName('head')[0]
  let el = document.createElement('link')
  el.setAttribute('type', 'text/css')
  el.setAttribute('rel', 'stylesheet')
  el.setAttribute('href', link)
  head.appendChild(el)
}

export function removeStyle(link) {
  let links = document.getElementsByTagName('link')
  for (let i = 0; i < links.length; i++) {
    if (links[i].getAttribute('href') === link) {
      links[i].parentNode.removeChild(links[i])
      break
    }
  }
}

export function removeAllThemeStyle() {
  const list = [
    params.resourseUrl + 'theme/theme_gold.css',
    params.resourseUrl + 'theme/theme_eye.css',
    params.resourseUrl + 'theme/theme_night.css',
    params.resourseUrl + 'theme/theme_default.css'
  ]
  for (let i in list) {
    removeStyle(list[i])
  }
}

export function changeFullTheme(theme) {
  let link = ''
  switch (theme) {
    case 'Gold':
      link = params.resourseUrl + 'theme/theme_gold.css'
      break
    case 'Eye':
      link = params.resourseUrl + 'theme/theme_eye.css'
      break
    case 'Night':
      link = params.resourseUrl + 'theme/theme_night.css'
      break
    default:
      link = params.resourseUrl + 'theme/theme_default.css'
  }
  addStyle(link)
}
